$(document).ready(function() {	
	$('.boxShoutbox .tipsy').tipsy({gravity: 'e'});
	$('.boxInvite .tipsy').tipsy({gravity: 'se',opacity:1});
	$("#frmInvite").validate({
		errorClass		: 'errorMessage',
		errorElement	: 'span',
		onfocusout		: false,
		onkeyup			: false,
		rules: {				
			piemail: {
				required	: true,
				email		: true
			}
		},
		messages: {	
			piemail: {
				required	: email_required,
				email		: email_invalid
			}
		},
		errorPlacement: function(label, element) {	
			label.insertAfter($('#frmInvite .btn'));			
		},
		submitHandler: function(form) {				
			form.submit();
		}
	});
	$("#frmShoutbox").validate({
		errorClass		: 'errorMessage',
		errorElement	: 'span',
		onfocusout		: false,
		onkeyup			: false,
		rules: {				
			pcontent: {
				required	: true,
				minlength	: 2
			}
		},
		messages: {	
			pcontent: {
				required	: content_sb_required,
				minlength	: content_sb_minlength
			}
		},
		errorPlacement: function(label, element) {	
			var id = element.attr('id');	
			label.insertAfter($('#' + id));			
		},
		submitHandler: function(form) {				
			var strContent	= $('#pcontent').val();
			strContent 		= encodeURIComponent(stripTags(strContent,-1));
			var sbDefault 	= $('#sbDefault').val()
			$.getJSON(				
				HOST_VFRIEND + 'comment/shoutbox?content='+strContent+'&callback=?',		
				function(response) {	
					var msg 	= response['msg'];
					var error	= response['error'];
					if(error == 0) {	
						$('#shoutbox').html(response['dshoutbox']);
						showUIMessage(msg);	
						if($.browser.msie) {
							$('#pcontent').css("min-height","22px");
							$('#pcontent').css("height","22px");
						} else {
							$('#pcontent').css("min-height","18px");
							$('#pcontent').css("height","18px");
						}
						$('#pcontent').val(sbDefault);
						$('#pcontent').css("color","#999");
						$('#scrollPane').jScrollPane().data('jsp').scrollToBottom();	
						$('.boxShoutbox .tipsy').tipsy({gravity: 'e'});
					} else {
						alert(msg);
					}
				}
			);
		}
	});
	$('#scrollPane').jScrollPane().data('jsp').scrollToBottom();
	$.cookie('INDEX_SHOUTBOX',1);
	$.cookie('INDEX_BEHAVIOUR',1);
	$.cookie('INDEX_COMMENT',1);
	$.cookie('MEMBER_NEW_BIE',1);	
	$.cookie('INDEX_NEWS', 1);
	$.cookie('INDEX_PLACE', 1);
	$(".boxMember").hoverIntent({					
		over: function(){
			$.cookie('MEMBER_NEW_BIE', 0);
		},					
		out: function(){	
			$.cookie('MEMBER_NEW_BIE', 1);
		}
	});
	$(".boxShoutbox").hoverIntent({					
		over: function(){
			$.cookie('INDEX_SHOUTBOX', 0);
		},					
		out: function(){	
			$.cookie('INDEX_SHOUTBOX', 1);
		}
	});
	/*$(".boxBehaviour").hoverIntent({					
		over: function(){
			$.cookie('INDEX_BEHAVIOUR', 0);
		},					
		out: function(){	
			$.cookie('INDEX_BEHAVIOUR', 1);
		}
	});
	setInterval('loadBehaviour(1)',1000 * 60 * 10);//10p	
	*/
	setInterval(function(){						 	
					loadShoutbox();
					$('.boxShoutbox .tipsy').tipsy({gravity: 'e'});
				},1000 * 10);
	setInterval('autoScroll()', 1000 * 10);
	$("#nSlider").easySlider({prevText:'',nextText:'',disable:true,controlsBefore:'<div class="bot">',controlsAfter:'</div>',speed:400});
	$("#pSlider").easySlider({prevId:'placePrevBtn',nextId:'placeNextBtn',prevText:'',nextText:'',disable:true,controlsBefore:'<div class="bot">',controlsAfter:'</div>',speed:400});
	$(window).scroll(function(){			
		if  ($(window).scrollTop() == $(document).height() - $(window).height()){
			loadBehaviour(0);
		}
	}); 
});
function loadNews(intType) {
	if($.cookie('INDEX_NEWS') == 1) {
		$.cookie('INDEX_NEWS', 0);
		$('.boxNews .loading').show();
		$.getJSON(				
			HOST_VFRIEND + 'ajax/news?act=4&nty='+intType+'&callback=?',		
			function(response) {	
				$('.boxNews .loading').hide();
				var msg 	= response['msg'];
				var error	= response['error'];
				if(error == 0) {	
					$.cookie('DASHBOARD_NEWS_TAB', intType);
					$('#divNews').html(response['dnews']);
					$('#headerNews').html(response['dheader']);
					$("#nSlider").easySlider({prevText:'',nextText:'',disable:true,controlsBefore:'<div class="bot">',controlsAfter:'</div>',speed:400});
				} else {
					alert(msg);
				}
				$.cookie('INDEX_NEWS', 1);
			}
		);
	}
}
function loadPlace(intType) {
	if($.cookie('INDEX_PLACE') == 1) {
		$.cookie('INDEX_PLACE', 0);
		$('.boxPlace .loading').show();
		$.getJSON(				
			HOST_VFRIEND + 'ajax/place?act=1&pty='+intType+'&callback=?',		
			function(response) {	
				$('.boxPlace .loading').hide();
				var msg 	= response['msg'];
				var error	= response['error'];
				if(error == 0) {
					$('#headerPlace').html(response['dheader']);											
					$('#divPlace').html(response['dplace']);					
					$("#pSlider").easySlider({prevId:'placePrevBtn',nextId:'placeNextBtn',prevText:'',nextText:'',disable:true,controlsBefore:'<div class="bot">',controlsAfter:'</div>',speed:400});
				} else {
					alert(msg);
				}
				$.cookie('INDEX_PLACE', 1);
			}
		);
	}
}
function autoScroll() {
	var allowScroll = $.cookie('MEMBER_NEW_BIE');
	if(allowScroll == 1) {
		var itemHeight = $('#newbie li').outerHeight();					
		/* calculte how much to move the scroller */
		var moveFactor = parseInt($('#newbie').css('top')) + itemHeight;
		/* animate the carousel */
		$('#newbie').animate({
			'top' : moveFactor
		}, 'slow', 'linear', function(){
			/* put the last item before the first item */
			$("#newbie li:first").before($("#newbie li:last"));
			/* reset top position */             
			$('#newbie').css({'top' : '0px'});
		});
	}
};
function loadBehaviour(intType) {	
	if(intType == 0) {
		var intPage = $('#pbepage').val();		
	} else {
		var intPage = 1;
	}
	if($.cookie('INDEX_BEHAVIOUR') == 1 && intPage > 0) {		
		$.cookie('INDEX_BEHAVIOUR',0);
		var intType = $('#stype').val();
		var intTime	= $('#sti').val();
		$('.boxBehaviour .loading').show();		
		$.getJSON(				
			HOST_VFRIEND + 'ajax/dashboard?act=7&sti='+intTime+'&stype='+intType+'&page='+intPage+'&callback=?',		
			function(response) {	
				var msg 	= response['msg'];
				var error	= response['error'];
				if(error == 0) {
					if(intType == 1) {
						$('.behaviourItem').html(response['dbehaviour']);
					} else {
						$('.boxBehaviour #pbepage').remove();
						$('.boxBehaviour .loading').remove();					
						$('.behaviourItem').append(response['dbehaviour']);
					}
				} else {
					alert(msg);
				}
				$.cookie('INDEX_BEHAVIOUR', 1);
			}
		);
	}
}
function loadCmt(intType,strId) {
	if($.cookie('INDEX_COMMENT') == 1) {
		$.cookie('INDEX_COMMENT', 0);
		$('#beh_sub_'+strId).show().html('<div class="loading"><img src="'+HOST_STATICS+'img/bg/loading-bar.gif" alt="Loading album for photo" width="88" height="7" border="0" /><br/>'+loading+'</div>');
		$.getJSON(				
			HOST_VFRIEND + 'ajax/dashboard?act=6&id='+strId+'&type='+intType+'&callback=?',		
			function(response) {	
				var msg 	= response['msg'];
				var error	= response['error'];
				if(error == 0) {
					$('#beh_'+strId).removeClass(" unRead");
					$('#squote_'+strId).removeClass(" uread");
					$('#quickCmt_'+strId).hide();
					$('#beh_sub_'+strId).html(response['dsub']);
				} else {
					alert(msg);
				}
				$.cookie('INDEX_COMMENT', 1);
			}
		);
	}
}
function loadShoutbox() {	
	var intLoad = $.cookie('INDEX_SHOUTBOX') ? $.cookie('INDEX_SHOUTBOX') : 1;
	if(intLoad == 1) {
		$.cookie('INDEX_SHOUTBOX',0);
		$.getJSON(				
			HOST_VFRIEND + 'ajax/shoutbox?callback=?',	
			function(response) {	
				var msg 	= response['msg'];
				var error	= response['error'];
				if(error == 0) {
					$('#shoutbox').html(response['dshoutbox']);
					if($('#scrollPane').jScrollPane().data('jsp') != undefined) {
						$('#scrollPane').jScrollPane().data('jsp').scrollToBottom();;					
					}					
				} else {
					alert(msg);
				}
				$.cookie('INDEX_SHOUTBOX',1);
			}
		);
	}
}
function like(intValue,intType,strId) {
	$.getJSON(				
		HOST_VFRIEND + 'ajax/dashboard?act=1&val='+intValue+'&type='+intType+'&id='+strId+'&callback=?',		
		function(response) {
			var msg 	= response['msg'];
			var error	= response['error'];
			if(error == 0) {						
				showUIMessage(msg);		
				var dInfo 	= response['dinfo'];
				var intLike	= response['like'];
				$('#beh_info_' + strId).html(dInfo);
				$('#beh_like_' + strId).html(intLike);
			} else {
				alert(msg);
			}
		}
	);
}
function like2(intValue,intType,strId) {
	$.getJSON(				
		HOST_VFRIEND + 'ajax/dashboard?act=8&val='+intValue+'&type='+intType+'&id='+strId+'&callback=?',		
		function(response) {
			var msg 	= response['msg'];
			var error	= response['error'];
			if(error == 0) {						
				showUIMessage(msg);		
				var dInfo 		= response['dinfo'];
				var intPoint	= response['point'];
				$('#beh_info_' + strId).html(dInfo);
				$('#beh_lnews_' + strId).html(intPoint);
			} else {
				alert(msg);
			}
		}
	);
}
function likeCmt(intParentId, intVal,intType, strId) {	
	$.getJSON(				
		HOST_VFRIEND + 'ajax/dashboard?act=2&parentid='+intParentId+'&val='+intVal+'&type='+intType+'&id='+strId+'&callback=?',		
		function(response) {	
			var msg 	= response['msg'];
			var error	= response['error'];
			if(error == 0) {						
				showUIMessage(msg);	
				var intLike = response['like'];
				var dInfo 	= response['dinfo'];
				$('#beh_info_' + strId).html(dInfo);
				$('#like_cmt_'+strId+'_'+intVal).html(intLike);
			} else {
				alert(msg);
			}
		}
	);
}
function postCmt(strDefault, strId, intValue, intType) {
	var strContent = $('#pcontent'+strId).val();
	if(strContent.toLowerCase() == strDefault.toLowerCase() || strContent == '') {
		if(intType == 3)
			alert(content_stt_required);
		else 
			alert(content_cmt_required);
		$('#pcontent'+strId).focus();
	} else {
		$('#frmCmt' + strId).hide();
		$('#loadingCmt'+strId).show();
		strContent = encodeURIComponent(strContent);
		$.getJSON(				
			HOST_VFRIEND + 'comment/dashboard?content='+strContent+'&id='+strId+'&val='+intValue+'&type='+intType+'&callback=?',		
			function(response) {	
				var msg 	= response['msg'];
				var error	= response['error'];
				if(error == 0) {						
					showUIMessage(msg);
					$('#beh_info_'+strId).html(response['dinfo']);
					$('#beh_cmt_'+strId).html(response['dcmt']);	
					if($.browser.msie) {
						$('#pcontent'+strId).css("min-height","22px");
						$('#pcontent'+strId).css("height","22px");
					} else {
						$('#pcontent'+strId).css("min-height","18px");
						$('#pcontent'+strId).css("height","18px");
					}
					$('#pcontent'+strId).val(strDefault);
					$('#pcontent'+strId).css("color","#999");
				} else {
					alert(msg);
				}
				$('#frmCmt' + strId).show();
				$('#loadingCmt'+strId).hide();
			}
		);
	} 
	return false;
}
function postCmtFck(strDefault, strId, intValue, intType) {
	var oEditorContent 	= FCKeditorAPI.GetInstance('pcontent' + strId) ;
	var strContent		= oEditorContent.GetXHTML( true );;
	var strContentSt	= html_entity_decode(stripTags(strContent,-1));	
	strDefault		 	= strDefault;	
	if(strContentSt == strDefault || strContentSt == '') {
		if(intType == 3)
			alert(content_stt_required);
		else 
			alert(content_cmt_required);		
		oEditorContent.Focus();
	} else {
		strContent = encodeURIComponent(stripTags(strContent,3));
		$('#frmCmt' + strId).hide();
		$('#loadingCmt'+strId).show();
		$.getJSON(				
			HOST_VFRIEND + 'comment/dashboard?content='+strContent+'&id='+strId+'&val='+intValue+'&type='+intType+'&callback=?',		
			function(response) {	
				var msg 	= response['msg'];
				var error	= response['error'];
				if(error == 0) {						
					showUIMessage(msg);
					$('#beh_info_'+strId).html(response['dinfo']);
					$('#beh_cmt_'+strId).html(response['dcmt']);	
					oEditorContent.SetHTML(strDefault);
				} else {
					alert(msg);
				}
				$('#frmCmt' + strId).show();
				$('#loadingCmt'+strId).hide();
			}
		);
	}
	return false;
}
function stopBehaviour(strId) {
	if(confirm(confirm_delete_behaviour) == true) {
		$.getJSON(				
			HOST_VFRIEND + 'ajax/dashboard?act=3&id='+strId+'&callback=?',		
			function(response) {	
				var msg 	= response['msg'];
				var error	= response['error'];
				if(error == 0) {						
					showUIMessage(msg);	
					$('#beh_'+strId).hide();
				} else {
					alert(msg);
				}
			}
		);
	}
}
function restoreBehaviour(strId) {
	$.getJSON(				
		HOST_VFRIEND + 'ajax/dashboard?act=4&id='+strId+'&callback=?',		
		function(response) {	
			var msg 	= response['msg'];
			var error	= response['error'];
			if(error == 0) {						
				showUIMessage(msg);	
				$('#beh_'+strId).html(response['data']);
			} else {
				alert(msg);
			}
		}
	);
}
function reportBehaviour(intValue, strId, intType, intStype, intPerm) {
	if(intPerm == 1) {
		var strConfirm = confirm_report_album_sex;
	} else if(intPerm == 2) {
		var strConfirm = confirm_report_album_pol;
	} else if(intPerm == 3) {
		var strConfirm = confirm_report_image_sex;
	} else if(intPerm == 4) {
		var strConfirm = confirm_report_image_pol;
	}
	if(confirm(strConfirm) == true) {
		$.getJSON(				
			HOST_VFRIEND + 'ajax/dashboard?act=5&type='+intType+'&stype='+intStype+'&perm='+intPerm+'&id='+strId+'&val='+intValue+'&callback=?',		
			function(response) {	
				var msg 	= response['msg'];
				var error	= response['error'];
				if(error == 0) {						
					showUIMessage(msg);	
					$('#beh_'+strId).hide();
				} else {
					alert(msg);
				}
			}
		);
	}
}
